import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_reader/model/novel.dart';
import 'package:flutter_reader/res/colors.dart';
import 'package:flutter_reader/ui/bookshelf/bookshelf_item_view.dart';
import 'package:flutter_reader/ui/home/home_page.dart';
import 'package:flutter_reader/utils/event_bus.dart';
import 'package:flutter_reader/utils/request.dart';
import 'package:flutter_reader/utils/screen.dart';
import 'package:flutter_reader/utils/toast.dart';

class BookshelfPage extends StatefulWidget {
  const BookshelfPage({Key key}) : super(key: key);

  @override
  _BookshelfPageState createState() => _BookshelfPageState();
}

class _BookshelfPageState extends State<BookshelfPage> with RouteAware {
  List<Novel> favoriteNovels = [];

  @override
  void initState() {
    super.initState();
    fetchData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: MyColors.white,
      appBar: AppBar(
        title: Text('书架',
            style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
        centerTitle: true,
        elevation: 0, // 隐藏底部阴影分割线
        actions: [
          Container(
            height: kToolbarHeight,
            width: 44,
            child: Image.asset('img/actionbar_search.png',
                color: MyColors.darkGray),
          ),
          Container(
            height: kToolbarHeight,
            width: 44,
            child:
                Image.asset('img/read_icon_more.png', color: MyColors.darkGray),
          ),
          SizedBox(width: 15),
        ],
      ),
      body: AnnotatedRegion(
        value: SystemUiOverlayStyle.dark,
        child: Column(
          children: [
            Expanded(
              child: RefreshIndicator(
                onRefresh: fetchData,
                child: ListView(
                  padding: EdgeInsets.only(top: 0),
                  children: [
                    _buildFavoriteView(),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildFavoriteView() {
    List<Widget> children = [];
    favoriteNovels.forEach((novel) {
      children.add(BookshelfItemView(novel));
    });
    var width = (Screen.width - 15 * 2 - 24 * 2) / 3;
    children.add(GestureDetector(
      onTap: () => EventBus.instance.emit(EventToggleTabBarIndex, 1),
      child: Container(
        color: MyColors.paper,
        width: width,
        height: width / 0.75,
        child: Image.asset('img/bookshelf_add.png'),
      ),
    ));
    return Container(
      padding: EdgeInsets.fromLTRB(15, 20, 15, 15),
      child: Wrap(
        spacing: 23,
        children: children,
      ),
    );
  }

  // 加载书籍列表
  Future<void> fetchData() async {
    try {
      List<Novel> favoriteNovels = await Request.getFavoriteNovels();
      setState(() {
        this.favoriteNovels = favoriteNovels;
      });
    } catch (e) {
      Toast.show(e.toString());
    }
  }
}
